<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 * Description of Server
 *
 * @author Administrator
 */

class Server extends CActiveRecord {

    public function rules() {
        return array(
            array('server_name, server_play,server_download', 'required'),
            array('server_name, server_play,server_download,server_parent_id', 'length', 'max' => 250),
            array('server_max_online', 'numerical', 'min' => 0, 'max' => 2000)
        );
    }
    public function attributeLabels() {
        return array(
            'server_id' => Yii::t('default', '编号'),
            'server_name' => Yii::t('default', '服务器名'),
             'server_play' => Yii::t('default', '点播服务器'),
            'server_download' => Yii::t('default', '下载服务器'),
            'server_max_online' => Yii::t('default', '并发上限'),
            'server_parent_id' => Yii::t('default', '所属服务器组'),
            'server_status' => Yii::t('default', '服务器状态'),
            'server_add_title' => Yii::t('default', '添加视频服务器信息'),
            'server_not_exists' => Yii::t('default', '服务器不存在'),
            'server_delete_failed' => Yii::t('default', '服务器删除失败,必须先移除子服务器才能删除'),
            'server_edit_title' => Yii::t('default', '编辑服务器资料'),
            'server_conflict_failed' => Yii::t('default', '不能选择自身作为服务器')
        );
    }
    /**
     * Returns the static model of the specified AR class.
     * @return CActiveRecord the static model class
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }
    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{server}}';
    }

    public function getParentOption() {
        $list = $this->findAll();
        $result = array();
        $result[0] = Yii::t('default', '设为主服务器');
        foreach ($list as $row) {
            $result[$row['server_id']] = $row['server_name'];
        }
        unset($list);
        return $result;
    }

    /**
     * 读取父分类的组名称
     * @param type $parentId
     * @return type 
     */
    public function getParentName($parentId) {
        if ($parentId > 0) {
            $result = $this->findByPk($parentId);
            if ($result) {
                return $result->server_name;
            } else {
                return Yii::t('default', '服务器组丢失');
            }
        } else {
            return Yii::t('default', '主服务器');
        }
    }

    public function getMaxOnlineLabel($num) {
        return $num > 0 ? $num : Yii::t('default', '<span style="color:#006934">无限制</span>');
    }
    public function getStatusArray() {
        return array('open' => Yii::t("default", "正常"), "closed" => Yii::t("default", "关闭"));
    }
    public function getStatusName($name) {
        $result = $this->getStatusArray();
        if (array_key_exists($name, $result)) {
            return $result[$name];
        } else {
            return Yii::t("default", "空");
        }
    }
    public function beforeDelete() {
        if (parent::beforeDelete()) {
            if ($this->count('server_parent_id = :id', array(':id' => $this->server_id)) > 0 ) {
                $this->addError('server_delete_failed', $this->getAttributeLabel('server_delete_failed'));
                return false;
            }
            return true;
        } else {
            return false;
        }
    }
    public function afterSave() {
        if(parent::afterSave()){
            if($this->isNewRecord===false){
                if( $this->server_parent_id == $this->server_id){
                     $this->addError('server_conflict_failed', $this->getAttributeLabel('server_conflict_failed'));
                    return false;
                }
            }
        }
    }
}

?>
